package com.zksk.admin.controller.customer;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.domain.customer.vo.CustomerProdReqFVo;
import com.zksk.admin.domain.customer.vo.CustomerProdReqVo;
import com.zksk.admin.service.customer.CustomerProdReqService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther xiebo
 * @date 2022/12/2
 **/
@Slf4j
@RestController
@RequestMapping("/customerProdReq/")
@Api(tags = "客户对产品需求相关接口")
public class CustomerProdReqController extends BaseController
{

    @Autowired
    private CustomerProdReqService customerProdReqService;

    @Log(title = "客户产品需求查询", businessType = BusinessType.SELECT)
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据ID查询产品需求信息")
    public R<CustomerProdReqVo> getById(@PathVariable Long id)
    {
        return customerProdReqService.selectById(id);
    }

    @Log(title = "客户产品需求查询", businessType = BusinessType.SELECT)
    @PostMapping("selectList")
    @ApiOperation(value = "分页查询产品需求信息")
    public R<Page<CustomerProdReqVo>> selectList(@RequestBody CustomerProdReqFVo customerProdReqFVo)
    {
        return customerProdReqService.selectList(customerProdReqFVo);
    }


    @Log(title = "客户产品需求新增", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @ApiOperation(value = "新增客户产品需求,返回新增ID")
    public R add(@RequestBody @Validated(Insert.class) CustomerProdReqVo customerProdReqVo)
    {
        if (CollUtil.isEmpty(customerProdReqVo.getAgreementVos()))
        {
            return R.fail("模板协议未选择!");
        }
        else if (CollUtil.isEmpty(customerProdReqVo.getRateList()))
        {
            return R.fail("费率未选择!");
        }
        else if (StrUtil.isEmpty(customerProdReqVo.getCaCompanyName()))
        {
            return R.fail("CA厂家未选择!");
        }
        else if (CollUtil.isEmpty(customerProdReqVo.getPaymentList()))
        {
            return R.fail("支付方式未选择!");
        }
        return customerProdReqService.insert(customerProdReqVo);
    }

    @Log(title = "客户产品需求删除", businessType = BusinessType.DELETE)
    @DeleteMapping("delete/{ids}")
    @ApiOperation(value = "批量删除客户的产品需求")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@PathVariable("ids") List<Long> ids)
    {
        return customerProdReqService.deletes(ids);
    }

    @Log(title = "客户产品需求修改", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    @ApiOperation(value = "修改客户产品需求,返回修改条数")
    public R update(@RequestBody @Validated(Update.class) CustomerProdReqVo customerProdReqVo)
    {
        if (CollUtil.isEmpty(customerProdReqVo.getAgreementVos()))
        {
            return R.fail("模板协议未选择!");
        }
        else if (CollUtil.isEmpty(customerProdReqVo.getRateList()))
        {
            return R.fail("费率未选择!");
        }
        else if (StrUtil.isEmpty(customerProdReqVo.getCaCompanyName()))
        {
            return R.fail("CA厂家未选择!");
        }
        else if (CollUtil.isEmpty(customerProdReqVo.getPaymentList()))
        {
            return R.fail("支付方式未选择!");
        }
        return customerProdReqService.updateCustomerProdReq(customerProdReqVo);
    }

}
